package org.rulix.ogame.player.core.http;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.http.HttpRequest;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.rulix.ogame.player.api.OgamePlanet;
import org.rulix.ogame.player.core.OgameException;
import org.rulix.ogame.player.core.OgamePaths;
import org.rulix.ogame.player.core.OgameSession;

public class OgameRequestFactory {
	
	public static HttpPost getLoginRequest(OgameSession ogameSession) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_LOGIN, ogameSession.getSessionId());
		sb.append(url);
		
		HttpPost httpost = new HttpPost(sb.toString());
           
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
		nvps.add(new BasicNameValuePair("uni_id", ""));
		nvps.add(new BasicNameValuePair("kid", ""));
		nvps.add(new BasicNameValuePair("is_utf8", "0"));
		nvps.add(new BasicNameValuePair("uni_url", ogameSession.getOgameServer()));
		nvps.add(new BasicNameValuePair("login", ogameSession.getLogin()));
		nvps.add(new BasicNameValuePair("pass", ogameSession.getPassword()));
 
		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			throw new OgameException("Cannot create login request", e);
		}
		return httpost;
	}
	
	public static HttpGet getOverviewRequest(OgameSession ogameSession) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_OVERVIEW);
		sb.append(url);
		
		HttpGet httpget = new HttpGet(sb.toString());
		return httpget;
	}
	
	public static HttpGet getOverviewPlanetRequest(OgameSession ogameSession, OgamePlanet planet) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_OVERVIEW_PLANET, planet.getCode());
		sb.append(url);
		
		HttpGet httpget = new HttpGet(sb.toString());
		return httpget;
	}
	
	public static HttpGet getBuildingsRequest(OgameSession ogameSession, OgamePlanet planet) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_RESOURCES, planet.getCode());
		sb.append(url);
		
		HttpGet httpget = new HttpGet(sb.toString());
		return httpget;
	}
	
	public static HttpGet getFacilitiesRequest(OgameSession ogameSession, OgamePlanet planet) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_STATION, planet.getCode());
		sb.append(url);
		
		HttpGet httpget = new HttpGet(sb.toString());
		return httpget;
	}
	
	public static HttpGet getResearchRequest(OgameSession ogameSession, OgamePlanet planet) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_RESEARCH, planet.getCode());
		sb.append(url);
		
		HttpGet httpget = new HttpGet(sb.toString());
		return httpget;
	}
	
	public static HttpGet getShipyardRequest(OgameSession ogameSession, OgamePlanet planet) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_SHIPYARD, planet.getCode());
		sb.append(url);
		
		HttpGet httpget = new HttpGet(sb.toString());
		return httpget;
	}
	
	public static HttpGet getDefenseRequest(OgameSession ogameSession, OgamePlanet planet) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_DEFENSE, planet.getCode());
		sb.append(url);
		
		HttpGet httpget = new HttpGet(sb.toString());
		return httpget;
	}
	
	
	
	public static HttpRequest getFetchReourcesRequest(OgameSession ogameSession) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
			String url = String.format(OgamePaths.OGAME_PATH_FETCH_RESOURCES, ogameSession.getSessionId());
			sb.append(url);

		HttpGet httpget = new HttpGet(sb.toString());
		return httpget;
	}
	
	public static HttpRequest getMessagesRequest(OgameSession ogameSession) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
			String url = String.format(OgamePaths.OGAME_PATH_MESSAGES, ogameSession.getSessionId());
			sb.append(url);
		
		HttpPost httpost = new HttpPost(sb.toString());
           
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
		nvps.add(new BasicNameValuePair("displayCategory", "9"));
		nvps.add(new BasicNameValuePair("displayPage", "1"));
		nvps.add(new BasicNameValuePair("ajax", "1"));

		try {
			httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			throw new OgameException("Cannot create messagess request", e);
		}
		return httpost;
	}
	
	/**



		**/
	private static List<NameValuePair> getFleetData(OgamePlanet ogamePlanet){
		
		List<NameValuePair> fleetData = Collections.synchronizedList(new ArrayList<NameValuePair>());
		if (ogamePlanet.getLightFighter() != 0){
			fleetData.add(new BasicNameValuePair("am204", ""+ogamePlanet.getLightFighter()));
		}
		if (ogamePlanet.getHeavyFighter() != 0){
			fleetData.add(new BasicNameValuePair("am205", ""+ogamePlanet.getHeavyFighter()));
		}
		if (ogamePlanet.getCruiser() != 0){
			fleetData.add(new BasicNameValuePair("am206", ""+ogamePlanet.getCruiser()));
		}
		if (ogamePlanet.getBattleShip() != 0){
			fleetData.add(new BasicNameValuePair("am207", ""+ogamePlanet.getBattleShip()));
		}
		if (ogamePlanet.getBattlecruiser() != 0){
			fleetData.add(new BasicNameValuePair("am215", ""+ogamePlanet.getBattlecruiser()));
		}
		if (ogamePlanet.getBomber() != 0){
			fleetData.add(new BasicNameValuePair("am211", ""+ogamePlanet.getBomber()));
		}
		if (ogamePlanet.getDestroyer() != 0){
			fleetData.add(new BasicNameValuePair("am213", ""+ogamePlanet.getDestroyer()));
		}
		if (ogamePlanet.getDeathStar() != 0){
			fleetData.add(new BasicNameValuePair("am214", ""+ogamePlanet.getDeathStar()));
		}
		if (ogamePlanet.getSmallCargo() != 0){
			fleetData.add(new BasicNameValuePair("am202", ""+ogamePlanet.getSmallCargo()));
		}
		if (ogamePlanet.getLargeCargo() != 0){
			fleetData.add(new BasicNameValuePair("am203", ""+ogamePlanet.getLargeCargo()));
		}
		if (ogamePlanet.getColonyShip() != 0){
			fleetData.add(new BasicNameValuePair("am208", ""+ogamePlanet.getColonyShip()));
		}
		if (ogamePlanet.getRecycler() != 0){
			fleetData.add(new BasicNameValuePair("am209", ""+ogamePlanet.getRecycler()));
		}
		if (ogamePlanet.getEspionageProbe() != 0){
			fleetData.add(new BasicNameValuePair("am210", ""+ogamePlanet.getEspionageProbe()));
		}
		return fleetData;
	}

	//OGAME_PATH_EVENTLIST
	public static HttpGet getEventListRequest(OgameSession ogameSession) throws OgameException {
		StringBuilder sb = new StringBuilder();
		sb.append("http://").
			append(ogameSession.getOgameServer());
			String url = String.format(OgamePaths.OGAME_PATH_EVENTLIST, ogameSession.getSessionId());
			sb.append(url);
		
		HttpGet httpGet = new HttpGet(sb.toString());
		return httpGet;
	}
	
	//Request for fleets
	public static HttpPost getFleet2Request(OgameSession ogameSession, OgamePlanet origin, OgamePlanet destination) throws OgameException {

		StringBuilder sb = new StringBuilder();
		sb.append("http://").
		append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_FLEET2, origin.getCode());
		sb.append(url);
		
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
		nvps.add(new BasicNameValuePair("galaxy", ""+origin.getCoordinates().getGalaxy()));
		nvps.add(new BasicNameValuePair("system", ""+origin.getCoordinates().getSolarSystem()));
		nvps.add(new BasicNameValuePair("position", ""+origin.getCoordinates().getPlanet()));
		nvps.add(new BasicNameValuePair("type", "1"));
		nvps.add(new BasicNameValuePair("mission", "0"));
		nvps.add(new BasicNameValuePair("speed", "10"));
		nvps.addAll(getFleetData(origin));
		
		HttpPost httpPost = new HttpPost(sb.toString());
 
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			throw new OgameException("Cannot create login request", e);
		}
		
		return httpPost;
	}
	
	//Request for fleets
	public static HttpPost getFleet3Request(OgameSession ogameSession, OgamePlanet origin, OgamePlanet destination) throws OgameException {

		StringBuilder sb = new StringBuilder();
		sb.append("http://").
		append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_FLEET3, origin.getCode());
		sb.append(url);
	
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
		nvps.add(new BasicNameValuePair("type", "1"));
		nvps.add(new BasicNameValuePair("mission", "0"));
		nvps.add(new BasicNameValuePair("union", "0"));
		nvps.addAll(getFleetData(origin));
		nvps.add(new BasicNameValuePair("galaxy", ""+destination.getCoordinates().getGalaxy()));
		nvps.add(new BasicNameValuePair("system", ""+destination.getCoordinates().getSolarSystem()));
		nvps.add(new BasicNameValuePair("position", ""+destination.getCoordinates().getPlanet()));
		nvps.add(new BasicNameValuePair("speed", "10"));
		
		HttpPost httpPost = new HttpPost(sb.toString());
 
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			throw new OgameException("Cannot create login request", e);
		}
		
		return httpPost;
	}
	
	//Request for fleets
	public static HttpPost getMovementRequest(OgameSession ogameSession, OgamePlanet origin, OgamePlanet destination) throws OgameException {

		StringBuilder sb = new StringBuilder();
		sb.append("http://").
		append(ogameSession.getOgameServer());
		String url = String.format(OgamePaths.OGAME_PATH_MOVEMENT, origin.getCode());
		sb.append(url);
		
		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
		nvps.add(new BasicNameValuePair("holdingtime", "1"));
		nvps.add(new BasicNameValuePair("expeditiontime", "1"));
		nvps.add(new BasicNameValuePair("galaxy", ""+destination.getCoordinates().getGalaxy()));
		nvps.add(new BasicNameValuePair("system", ""+destination.getCoordinates().getSolarSystem()));
		nvps.add(new BasicNameValuePair("position", ""+destination.getCoordinates().getPlanet()));
		nvps.add(new BasicNameValuePair("mission", "4"));
		nvps.add(new BasicNameValuePair("union2", "0"));
		nvps.add(new BasicNameValuePair("holdingOrExpTime", "0"));
		nvps.add(new BasicNameValuePair("speed", "10"));
		nvps.addAll(getFleetData(origin));
		nvps.add(new BasicNameValuePair("metal", "50000"));
		nvps.add(new BasicNameValuePair("crystal", "50000"));
		nvps.add(new BasicNameValuePair("deuterium", "50000"));
		
		HttpPost httpPost = new HttpPost(sb.toString());
 
		try {
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
		} catch (UnsupportedEncodingException e) {
			throw new OgameException("Cannot create login request", e);
		}
		
		return httpPost;
	}
	
}
